﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Bon_It.Model;

namespace Bonit_It.ViewModel.Base
{

    /// <summary>
    /// The handler for the workspace.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="viewModel"></param>
    public delegate void AttachWorkspaceHandler(object sender, Base.ViewModel viewModel);

    /// <summary>
    /// The generic ViewModel Interface
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class ViewModel : IDisposable, INotifyPropertyChanged, IView
    {
        /// <summary>
        /// The displayname for the tab.
        /// </summary>
        public string DisplayName { get; set; }

        /// <summary>
        /// The ctor.
        /// </summary>
        public ViewModel()
        {
            DeleteCommand = new RelayCommand((OnDelete), CanDelete);
            CreateCommand= new RelayCommand(OnCreate, CanCreate);
            SaveCommand = new RelayCommand(OnSave, CanSave);

        }
        

        /// <summary>
        /// Refresh the Binding list lookups.
        /// </summary>
        public abstract void RefreshLookups();

        /// <summary>
        /// Perform disposing methods.
        /// </summary>
        public abstract void CleanUp();


        #region Virtual Methods


        /// <summary>
        /// Execute some things after the save routines
        /// </summary>
        public virtual void AfterSave()
        {
            MessageBox.Show("Daten erfolgreich gespeichert", "Hinweis", MessageBoxButton.OK, MessageBoxImage.Information);
            // closing the current workspace
            this.OnRequestClose();
        }

        /// <summary>
        /// Executes some things before the delete occurs.
        /// </summary>
        /// <returns></returns>
        public virtual MessageBoxResult BeforeDelete()
        {
            return MessageBox.Show("Wollen Sie den gewählten Datensatz löschen?", "Hinweis", MessageBoxButton.YesNo, MessageBoxImage.Question);
        }

        #endregion


        #region Implementation of IDisposable

        /// <summary>
        /// cleaning up
        /// </summary>
        public void Dispose()
        {
            CleanUp();
        }

        #endregion

        #region Implementation of INotifyPropertyChanged

        /// <summary>
        /// Called when [property changed].
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        public void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion




        public abstract bool CanDelete(object argument);

        public abstract bool CanSave(object argument);

        public abstract bool CanCreate(object argument);

        public virtual void OnDelete(object argument)
        {
            BeforeDelete();
        }

        public virtual void OnSave(object argument)
        {
            AfterSave();
        }


        public virtual void OnCreate(object argument)
        {

        }

        /// <summary>
        /// Perform the delete.
        /// </summary>
        public ICommand DeleteCommand { get; set; }


        /// <summary>
        /// Perform the save.
        /// </summary>
        public ICommand SaveCommand { get; set; }

        /// <summary>
        /// Create command fpr the toolbar button.
        /// </summary>
        public ICommand CreateCommand { get; set; }

        private ICommand _closeCommand;

        /// <summary>
        /// Returns the command that, when invoked, attempts
        /// to remove this workspace from the user interface.
        /// </summary>
        public ICommand CloseCommand
        {
            get
            {
                if (_closeCommand == null)
                {
                    _closeCommand = new RelayCommand(param => this.OnRequestClose());
                }
                return _closeCommand;
            }
        }



        #region events
        protected void OnAttachWorkspace(ViewModel model)
        {
            AttachWorkspaceHandler handler = this.AttachWorkspace;
            if (handler != null)
            {
                handler(this, model);
                EventHandler handler1 = null;
                handler1 = delegate
                    {
                        model.RequestClose -= handler1;
                        this.RefreshLookups();

                    };
                model.RequestClose += handler1;
            }
        }


        protected void OnRequestClose()
        {
            EventHandler handler = this.RequestClose;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }

        
        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Raised when this workspace should be removed from the UI.
        /// </summary>
        [field: NonSerialized]
        public event EventHandler RequestClose;

        /// <summary>
        /// Attach the new form to the Workspace
        /// </summary>
        [field: NonSerialized]
        public event AttachWorkspaceHandler AttachWorkspace;

        #endregion

    }
        
}
